/*
 * @Author: LVGRAPE
 * @Date: 2023-09-08 17:56:24
 * @LastEditTime: 2024-05-18 16:35:55
 * @LastEditors: lvgrape lvgrape@outlook.com
 * @Description:
 * @FilePath: \zino-fc-v4\ZINO\rc\rc.c
 * 要啥没啥，爱咋咋的
 */

#include "drv_common.h"
// #include <nrf24l01.h>
#include <rtconfig.h>
#include <rtthread.h>
#include "rc.h"
#include "buzzer.h"
#include "zino.h"
#include "maths.h"
#include "rgb.h"
#include "buzzer.h"
// #include "fc_error.h"
#define DBG_TAG "2.4G"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>

#define CRC_MAGICBEE 0XED
#define SI24R1_SPI_DEV_NAME "spi_nrf"
#define SI24R1_CE_PIN GET_PIN(C, 4)
#define SI24R1_IRQ_PIN GET_PIN(C, 5)

#define RC_HOPPING_ENABLE 0

ZINO_CONFIG_REGISTER(struct rc_pair_data_s, pair_data, 3, 1);
ZINO_COMMON_RC_CHANNEL_s zinoCommonRcData;
rt_uint32_t fc_error_flag = 0;
enum wl_msg_head
{
    WL_MSG_HEAD_NULL = 0,
    WL_MSG_HEAD_RC,          // rc 通道数据
    WL_MSG_HEAD_PAIR_REQ,    // 对频请求
    WL_MSG_HEAD_PAIR_COMFIR, // 对频确认
    WL_MSG_HEAD_FC_STATUS,   // 飞控状态

};
// const static char* ROLE_TABLE[] = { "PTX", "PRX" };
struct rc_handle rchandle =
    {
        .pair_addr = "UAVI1",
        .pair_channel = 33,
        .pair_fc_handshake_byte = 0xfc,
        .pair_rc_handshake_byte = 0x9c,
        .pair_handshake_byte = 0x0A,
        .pair_handshake_byte_v1 = 0x1A,
        .pair_start_time = 0,
        .pair_wait_time = 30,
        .pairConfirmTime = 0,
        .address = {1, 2, 3, 4, 5},
        .rf_channel = 33,
        .min_channel = 2,
        .max_channel = 80,

};
/**
 * @brief 接收完成处理函数
 *
 * @param nrf24
 * @param data
 * @param len
 * @param pipe
 */
static void rx_ind(struct si24r1 *s, uint8_t *data, uint8_t len, int pipe)
{
    /*! Don't need to care the pipe if the role is ROLE_PTX */
    rt_kprintf("(p%d)_%d: ", pipe, len);
}

/**
 * @brief 发送完成处理函数
 *
 * @param nrf24
 * @param pipe
 */
static void tx_done(struct si24r1 *s, int pipe)
{
    static int cnt = 0;
    // static char tbuf[32];

    cnt++;

    /*! Here just want to tell the user when the role is ROLE_PTX
    the pipe have no special meaning except indicating (send) FAILED or OK
        However, it will matter when the role is ROLE_PRX*/
    s->port.ops->write_reg(&s->port, SI24R1_REG_CONFIG, (uint8_t *)&(s->onchip_config.config), 1);
    if (s->user_config.role == SI24R1_ROLE_TX)
    {
        if (pipe == 8)
            rt_kprintf("tx_done failed");
        else
            rt_kprintf("tx_done ok");
    }
    else
    {
        rt_kprintf("tx_done ok");
    }

    rt_kprintf(" (pipe%d)\n", pipe);
    rchandle.state = RC_TRANSMITTING;
    if (rchandle.mode == RC_MODE_PAIRING)
    {
        rt_kprintf("pairing, retry...\n");
        si24r1_send_data(s, rchandle.tx_msg.raw, RTX_BUFFER_SIZE, 0); // 8 bytes ack enough, or would lost signal
    }
}

static struct si24r1_callback si24r1_cb = {
    .rx_ind = rx_ind,
    .tx_done = tx_done,
};
/**
 * @brief 摇控器si24r1的配置，
 * 信号丢失时，参数变化时，需要调用一下来更新si24r1的寄存器配置
 *
 * @param rc
 */
void rc_si24r1_setting(struct rc_handle *rc)
{

    // #if 0
    //     rt_memcpy(rc->address, "test3", 5);
    //     rc->rf_channel = 125;
    // #else
    //     rt_memcpy(rc->address, "test4", 5);
    //     rc->rf_channel = 130;
    // #endif
    rt_memset(&rc->si24r1.onchip_config, 0, sizeof(rc->si24r1.onchip_config));
    if (rc->mode == RC_MODE_PAIRING)
    {
        rt_memcpy(rc->si24r1.onchip_config.tx_addr.addr, rc->pair_addr, 5);
        rt_memcpy(rc->si24r1.onchip_config.rx_addr_p0.addr, rc->pair_addr, 5);
        rc->si24r1.onchip_config.rf_ch.RF_CH = rc->pair_channel;
    }
    else
    {
        rt_memcpy(rc->si24r1.onchip_config.tx_addr.addr, rc->address, 5);
        rt_memcpy(rc->si24r1.onchip_config.rx_addr_p0.addr, rc->address, 5);
        rc->si24r1.onchip_config.rf_ch.RF_CH = rc->rf_channel;
    }

    rc->si24r1.onchip_config.en_rxaddr.ERX_P0 = 1;
    rc->si24r1.onchip_config.rx_pw_p0.RX_PW_P0 = RTX_BUFFER_SIZE;
    rc->si24r1.onchip_config.setup_aw.AW = 0x03;
    rc->si24r1.onchip_config.feature.EN_DPL = 1;
    rc->si24r1.onchip_config.feature.EN_ACK_PAYd = 1;
    rc->si24r1.onchip_config.rf_setup.RF_DR_HIGH = 1;
    rc->si24r1.onchip_config.rf_setup.RF_PWR = 6;
    rc->si24r1.onchip_config.config.EN_CRC = 1;
    rc->si24r1.onchip_config.config.CRCO = 1;
    rc->si24r1.onchip_config.config.PWR_UP = 1;
    rc->si24r1.onchip_config.config.PRIM_RX = 0; // RX:1,TX:0
    rc->si24r1.onchip_config.dynpd.DPL_P0 = 1;

    rc->si24r1.onchip_config.status.MAX_RT = 1; // 清除中断
    rc->si24r1.onchip_config.status.RX_DR = 1;  // 清除中断
    rc->si24r1.onchip_config.status.TX_DS = 1;  // 清除中断
    rc->si24r1.onchip_config.en_aa.ENAA_P0 = 1;
    rc->si24r1.onchip_config.setup_retr.ARC = 10; // 10次
    rc->si24r1.onchip_config.setup_retr.ARD = 0;  // 250us

    rc->si24r1.user_config.role = SI24R1_ROLE_TX;

    si24r1_update_onchip_config(&rc->si24r1);
    // if (rc->mode == RC_MODE_PAIRING)
    //     si24r1_check_all_reg(&rc->si24r1);
}
#define PKG_SI24R1_VERSION "1.0.0"
static void rc_second_timer_cb(void *parameter)
{
    struct rc_handle *rc = (struct rc_handle *)parameter;
    rc->rx_rssi = rc->rxCnt;
    rc->rxCnt = 0;
    // LOG_D("RC:%dHz, %d FC:0x%08X\n",
    //       rc->rx_rssi, rc->lost_cnt, fc_error_flag);
    rc->lost_cnt++;
    if (rc->lost_cnt > 1)
    {
        LOG_D("rc lost, restart si24r1\n");
        // rt_sem_release(rc->si24r1.sem);
    }

    if (rc->rx_rssi < 5)
    {
        if (rc->mode != RC_MODE_PAIRING)
        {
            ZINO_STATE_SET(ZINO_RC_LOST);            
        }
    }
    else
    {
        ZINO_STATE_CLR(ZINO_RC_LOST);
        if(rc->mode != RC_MODE_PAIRING)
        {
            ZINO_STATE_SET(ZINO_STANDBY);            
        }
    }
}
void rc_rx_handle(struct rc_handle *rc)
{
    // rt_kprintf("rc rx handle:");
    // for(uint8_t i=0; i<8; i++)
    // {
    //     rt_kprintf("0x%02X ", rc->rx_msg.raw[i]);
    // }
    // rt_kprintf("\n");
    if (rc->mode == RC_MODE_PAIRING)
    {
        if (rc->rx_msg.raw[0] == WL_MSG_HEAD_PAIR_REQ)
        {
            if (rc->pairOk != 1)
            {
                LOG_D("pairing ok!");
                if (strncmp((const char *)&rc->rx_msg.raw[2], (const char *)rc->newAddress, 5) == 0 && rc->rx_msg.raw[7] == rc->newRfChannel)
                {
                    rc->pairOk = 1;
                    rc->tx_cnt = 0;
                    LOG_D("pairing ok2!");
                }
            }
        }
        else if (rc->rx_msg.raw[0] == WL_MSG_HEAD_PAIR_COMFIR)
        {
            if (rc->rx_msg.raw[1] >= 10 && rc->pairOk)
            {
                LOG_D("req and comfirm, pair done!\n");
                rc->address[0] = rc->newAddress[0];
                rc->address[1] = rc->newAddress[1];
                rc->address[2] = rc->newAddress[2];
                rc->address[3] = rc->newAddress[3];
                rc->address[4] = rc->newAddress[4];
                rc->rf_channel = rc->newRfChannel;
                rc->pair_start_time = 0;
                // NOTE save paired data
                pair_data()->flag = 0XDD;
                pair_data()->address[0] = rc->address[0];
                pair_data()->address[1] = rc->address[1];
                pair_data()->address[2] = rc->address[2];
                pair_data()->address[3] = rc->address[3];
                pair_data()->address[4] = rc->address[4];
                pair_data()->rf_channel = rc->rf_channel;

#if RC_HOPPING_ENABLE
                rc->hopping[0] = (rc->address[0] / 2 + rc->min_channel) % rc->max_channel;
                rc->hopping[1] = (rc->address[1] / 2 + rc->min_channel) % rc->max_channel;
                rc->hopping[2] = (rc->address[2] / 2 + rc->min_channel) % rc->max_channel;
                rc->hopping[3] = (rc->address[3] / 2 + rc->min_channel) % rc->max_channel;
                rc->hopping[4] = (rc->address[4] / 2 + rc->min_channel) % rc->max_channel;
                rc->hopping_cnt = 0;
                rc->rf_channel = rc->hopping[rc->hopping_cnt];
                LOG_D("Hopping:%02d-%02d-%02d-%02d-%02d\n", rc->hopping[0], rc->hopping[1], rc->hopping[2], rc->hopping[3], rc->hopping[4]);
#endif
                zino_config_save_all();
                const uint8_t pairOkTone[] = {SO_H, LA_H, SI_H};
                buzzer_play_note(pairOkTone, 3);
                ZINO_STATE_CLR(ZINO_RC_PAIRING);
                rc->mode = RC_MODE_NORMAL;
                
                rc_si24r1_setting(rc);
                si24r1_check_all_reg(&rc->si24r1);                
            }
        }
    }
    else
    {
        fc_error_flag = rc->rx_msg.raw[2];
        fc_error_flag <<= 8;
        fc_error_flag |= rc->rx_msg.raw[3];
        fc_error_flag <<= 8;
        fc_error_flag |= rc->rx_msg.raw[4];
        fc_error_flag <<= 8;
        fc_error_flag |= rc->rx_msg.raw[5];
        fc_error_flag <<= 8;
        // LOG_D("fc_error_flag = %lx\r\n",fc_error_flag);
    }
}
static void si24r1_thread(void *param)
{
    rt_err_t ret = RT_EOK;
    struct rc_handle *rc = (struct rc_handle *)param;
    LOG_I("2.4G wireless handle:%s\n", PKG_SI24R1_VERSION);
    do
    {
        ret = si24r1_init(&rc->si24r1, SI24R1_SPI_DEV_NAME, SI24R1_CE_PIN, SI24R1_IRQ_PIN, &si24r1_cb);
        if (ret != RT_EOK)
        {
            LOG_E("Failed to init si24r1. Next try in 3 second.");
            rt_thread_mdelay(3000);
        }
    } while (ret != RT_EOK);
    LOG_I("si24r1_thread running...");
    rc->si24r1.flags.using_irq = 1;

    rc->second_timer = rt_timer_create("rc_1s", rc_second_timer_cb, rc, 1000, RT_TIMER_FLAG_PERIODIC);
    rt_timer_start(rc->second_timer);

    rc_si24r1_setting(rc);
    si24r1_check_all_reg(&rc->si24r1);
    while (1)
    {
        switch (rc->state)
        {
        case RC_TRANSMITTING:
            if (rc->mode == RC_MODE_PAIRING)
            {
                memset(rc->tx_msg.raw, 0, sizeof(rc->tx_msg.raw));
                rc->tx_msg.raw[0] = ((rc->pairOk != 1) ? WL_MSG_HEAD_PAIR_REQ : WL_MSG_HEAD_PAIR_COMFIR);
                rc->tx_msg.raw[1] = rc->tx_cnt;
                rc->tx_msg.raw[2] = rc->newAddress[0];
                rc->tx_msg.raw[3] = rc->newAddress[1];
                rc->tx_msg.raw[4] = rc->newAddress[2];
                rc->tx_msg.raw[5] = rc->newAddress[3];
                rc->tx_msg.raw[6] = rc->newAddress[4];
                rc->tx_msg.raw[7] = rc->newRfChannel;
                rc->tx_msg.v.crc = crc8_dvb_s2_update(CRC_MAGICBEE, rc->tx_msg.raw, RTX_BUFFER_SIZE - 1);
                // LOG_D("pair tx %d %d",rc->pairOk, rc->tx_msg.raw[0]);
            }
            else
            {
                zinoCommonRcData.values.index = rc->tx_cnt;
                zinoCommonRcData.values.head = WL_MSG_HEAD_RC;
                rt_memcpy(rc->tx_msg.raw, zinoCommonRcData.raw, sizeof(zinoCommonRcData));
                // rc->tx_msg.v.head = WL_MSG_HEAD_RC;
                // rc->tx_msg.v.index = rc->tx_cnt;
                // rc->tx_msg.v.channel[0] = zinoCommonRcData.values.;
                // rc->tx_msg.v.channel[1] = 0;
                // rc->tx_msg.v.channel[2] = 0;
                // rc->tx_msg.v.channel[3] = 0;
                // rc->tx_msg.v.channel[4] = 0;
                // rc->tx_msg.v.channel[5] = 0;
                // rc->tx_msg.v.channel[6] = 0;
                // rc->tx_msg.v.channel[7] = 0;
                // LOG_D("nor tx");
                rc->tx_msg.v.crc = crc8_dvb_s2_update(CRC_MAGICBEE, rc->tx_msg.raw, RTX_BUFFER_SIZE - 1);

#if RC_HOPPING_ENABLE
                rc->hopping_cnt++;
                if (rc->hopping_cnt >= 5)
                {
                    rc->hopping_cnt = 0;
                }
                rc->rf_channel = rc->hopping[rc->hopping_cnt];
                rc->si24r1.onchip_config.rf_ch.RF_CH = rc->rf_channel;
                rc->si24r1.port.ops->write_reg(&rc->si24r1.port, SI24R1_REG_RF_CH, (uint8_t *)&rc->si24r1.onchip_config.rf_ch, 1);
                // LOG_D("hopping %d", rc->rf_channel);
#endif
            }
            /**TX mode*/
            rc->si24r1.onchip_config.config.PRIM_RX = 0;
            rc->si24r1.onchip_config.config.MASK_MAX_RT = 0;
            rc->si24r1.onchip_config.config.MASK_RX_DR = 0;
            rc->si24r1.onchip_config.config.MASK_TX_DS = 0;
            rc->si24r1.port.ops->write(&rc->si24r1.port, SI24R1_CMD_FLUSH_TX, 0, 0);
            rc->si24r1.port.ops->write_reg(&rc->si24r1.port, SI24R1_REG_CONFIG, (uint8_t *)&rc->si24r1.onchip_config.config, 1);
            si24r1_send_data(&rc->si24r1, rc->tx_msg.raw, RTX_BUFFER_SIZE, 0);
            rc->tx_cnt++;
            // LOG_D("tx %d", rc->rf_channel);
            rc->state = RC_WAIT_ACK;
            break;
        case RC_TRANSMITTED:
            break;

        case RC_RECEIVING:
            break;

        case RC_WAIT_ACK:
            /**wait irq */
            if (rc->si24r1.flags.using_irq)
            {
                rt_sem_take(rc->si24r1.sem, RT_WAITING_FOREVER);
            }
            /**read status */
            rc->lost_cnt = 0;
            rc->si24r1.port.ops->read_reg(&rc->si24r1.port, SI24R1_REG_STATUS, (uint8_t *)&rc->si24r1.onchip_config.status, 1);
            /**clear irq */
            rc->si24r1.port.ops->write_reg(&rc->si24r1.port, SI24R1_REG_STATUS, (uint8_t *)&rc->si24r1.onchip_config.status, 1);
            // LOG_D("status:%02x",*(uint8_t *)&rc->si24r1.onchip_config.status);
            if (rc->si24r1.onchip_config.status.MAX_RT)
            {
                // LOG_D("MAX_RT,%d", rc->rf_channel);
                rc->state = RC_TRANSMITTING;
                rt_thread_mdelay(14);
            }
            if (rc->si24r1.onchip_config.status.RX_DR)
            {
                uint8_t len;
                rc->state = RC_TRANSMITTING;
                rc->si24r1.port.ops->read(&rc->si24r1.port, SI24R1_CMD_R_RX_PL_WID, &len, 1);
                rc->si24r1.port.ops->read(&rc->si24r1.port, SI24R1_CMD_R_RX_PAYLOAD, rc->rx_msg.raw, len);
                rc->si24r1.port.ops->write(&rc->si24r1.port, SI24R1_CMD_FLUSH_RX, 0, 0);
                // LOG_D("RX_DR:%d,%d", (int)len, rc->rf_channel);
                // for(uint8_t i=0;i<len;i++)
                // {
                //     rt_kprintf("0x%02x ",rc->rx_msg.raw[i]);
                // }
                rc->rxCnt++;
                rc_rx_handle(rc);
            }
            if (rc->si24r1.onchip_config.status.TX_DS)
            {
                // LOG_D("TX_DS,%d", rc->rf_channel);
                rt_thread_mdelay(14);
            }
            rc->state = RC_TRANSMITTING;
            // si24r1_run(&rc->si24r1);
            break;

        default:
            break;
        }

        // si24r1_run(si);
        // if (!si->flags.using_irq)
        // {
        //     rt_thread_mdelay(100);
        // }
    }
    // return 0;
}
/**
 * @brief 20ms task
 *
 * @param trigger
 */
void rc_pairing_trigger(uint8_t trigger)
{
    if (trigger)
    {
        if (rchandle.pairCooling == 0)
        {
            if (rchandle.pairTrigger < 50)
            {
                rchandle.pairTrigger++;
            }
            else
            {
                rchandle.pairCooling = 200;
                if (rchandle.mode != RC_MODE_PAIRING)
                    rc_start_pairing();
                else
                    rc_stop_pairing();
            }
        }
    }
    else
    {
        rchandle.pairTrigger = 0;
        if (rchandle.pairCooling > 0)
            rchandle.pairCooling--;
    }
}
void rc_stop_pairing()
{
    if (rchandle.mode != RC_MODE_NORMAL)
    {
        rchandle.mode = RC_MODE_NORMAL;
        ZINO_STATE_CLR(ZINO_RC_PAIRING);
        rc_si24r1_setting(&rchandle);
        LOG_D("pairing stop");
        vibration_set(50, 100);
        rt_thread_mdelay(50);
        vibration_set(50, 100);
    }
}
void rc_start_pairing(void)
{
    if (rchandle.mode != RC_MODE_PAIRING)
    {
        rchandle.mode = RC_MODE_PAIRING;
        rchandle.pair_start_time = millis();
        srand(millis());
        while (1)
        {
            rchandle.newRfChannel = rand() % 125;
            if (rchandle.newRfChannel >= rchandle.min_channel && rchandle.newRfChannel <= rchandle.max_channel)
            {
                break;
            }
        }
        for (uint8_t i = 0; i < 5; i++)
        {
            rchandle.newAddress[i] = rand() % 255;
            LOG_D("newRandAddr[%d]:%02X", (int)i, (int)rchandle.newAddress[i]);
        }
        LOG_D("new ch:%02X", (int)rchandle.newRfChannel);
        memcpy(rchandle.address, rchandle.pair_addr, 5);
        rchandle.rf_channel = rchandle.pair_channel;
        rchandle.pairOk = 0;
        rchandle.tx_msg.raw[0] = ((rchandle.pairOk != 1) ? WL_MSG_HEAD_PAIR_REQ : WL_MSG_HEAD_PAIR_COMFIR);
        rchandle.tx_msg.raw[1] = rchandle.tx_cnt;
        rchandle.tx_msg.raw[2] = rchandle.newAddress[0];
        rchandle.tx_msg.raw[3] = rchandle.newAddress[1];
        rchandle.tx_msg.raw[4] = rchandle.newAddress[2];
        rchandle.tx_msg.raw[5] = rchandle.newAddress[3];
        rchandle.tx_msg.raw[6] = rchandle.newAddress[4];
        rchandle.tx_msg.raw[7] = rchandle.newRfChannel;

        // rchandle.hopping[0] = (rchandle.newAddress[0]/2 + rchandle.min_channel)%rchandle.max_channel;
        // rchandle.hopping[1] = (rchandle.newAddress[1]/2 + rchandle.min_channel)%rchandle.max_channel;
        // rchandle.hopping[2] = (rchandle.newAddress[2]/2 + rchandle.min_channel)%rchandle.max_channel;
        // rchandle.hopping[3] = (rchandle.newAddress[3]/2 + rchandle.min_channel)%rchandle.max_channel;
        // rchandle.hopping[4] = (rchandle.newAddress[4]/2 + rchandle.min_channel)%rchandle.max_channel;
        // LOG_D("Hopping:%02d-%02d-%02d-%02d-%02d\n",
        // rchandle.hopping[0],
        // rchandle.hopping[1],
        // rchandle.hopping[2],
        // rchandle.hopping[3],
        // rchandle.hopping[4]);

        rchandle.tx_msg.v.crc = crc8_dvb_s2_update(CRC_MAGICBEE, rchandle.tx_msg.raw, RTX_BUFFER_SIZE - 1);
        rc_si24r1_setting(&rchandle);
        LOG_D("start pairing");
        ZINO_STATE_CLR(ZINO_RC_LOST);
        ZINO_STATE_SET(ZINO_RC_PAIRING);
        vibration_set(50, 100);
    }
    // else
    // {
    //     if(millis() > (rchandle.pair_start_time + 3000) && rchandle.pair_start_time!=0)
    //     {
    //         rchandle.mode = RC_MODE_NORMAL;
    //         ZINO_STATE_CLR(ZINO_RC_PAIRING);
    //         LOG_D("stop pairing, normal mode \n\r");
    //     }
    // }
    // rt_event_send(rchandle.event, RC_EVENT_START_PAIRING);
}

static int rc_task_init(void)
{

    rt_thread_t thread1;
    if (pair_data()->flag == 0xdd)
    {
        rchandle.address[0] = pair_data()->address[0];
        rchandle.address[1] = pair_data()->address[1];
        rchandle.address[2] = pair_data()->address[2];
        rchandle.address[3] = pair_data()->address[3];
        rchandle.address[4] = pair_data()->address[4];
        rchandle.rf_channel = pair_data()->rf_channel;
        LOG_D("Loading save data:\n");
        LOG_D("address[0] = %x", rchandle.address[0]);
        LOG_D("address[1] = %x", rchandle.address[1]);
        LOG_D("address[2] = %x", rchandle.address[2]);
        LOG_D("address[3] = %x", rchandle.address[3]);
        LOG_D("address[4] = %x", rchandle.address[4]);
        LOG_D("rf_channel = %x", rchandle.rf_channel);
    }
#if RC_HOPPING_ENABLE
    rchandle.hopping[0] = (rchandle.address[0] / 2 + rchandle.min_channel) % rchandle.max_channel;
    rchandle.hopping[1] = (rchandle.address[1] / 2 + rchandle.min_channel) % rchandle.max_channel;
    rchandle.hopping[2] = (rchandle.address[2] / 2 + rchandle.min_channel) % rchandle.max_channel;
    rchandle.hopping[3] = (rchandle.address[3] / 2 + rchandle.min_channel) % rchandle.max_channel;
    rchandle.hopping[4] = (rchandle.address[4] / 2 + rchandle.min_channel) % rchandle.max_channel;
    rchandle.hopping_cnt = 0;
    rchandle.rf_channel = rchandle.hopping[rchandle.hopping_cnt];
    LOG_D("Hopping:%d-%d-%d-%d-%d\n", rchandle.hopping[0], rchandle.hopping[1], rchandle.hopping[2], rchandle.hopping[3], rchandle.hopping[4]);
#endif

    thread1 = rt_thread_create("2.4G wl", si24r1_thread, &rchandle, 512, RT_THREAD_PRIORITY_MAX / 2, 20);
    if (thread1 != RT_NULL)
        rt_thread_startup(thread1);
    else
        LOG_E("2.4G wl task creat fail!\n");

    return RT_EOK;
}
void rc_set(int arg_c, char **argv)
{
    if (rt_strcmp(argv[1], "pair") == 0)
    {
        rc_start_pairing();
    }
    else if (rt_strcmp(argv[1], "channel") == 0)
    {
        rchandle.rf_channel = atoi(argv[2]);
        LOG_D("set rf channel:%d", rchandle.rf_channel);
        rc_si24r1_setting(&rchandle);
        si24r1_check_all_reg(&rchandle.si24r1);
    }
    else
    {
        rt_kprintf("Usage:\n");
        rt_kprintf("rc_set pair\n");
        rt_kprintf("rc_set channel <channel>\n");
    }
}

uint16_t get_rc_channel(uint8_t rcchannel)
{
    if (rcchannel < 8)
        return rchandle.rx_msg.v.channel[rcchannel];
    return 0;
}

struct rc_handle *get_rc_channel_ram(void)
{
    return &rchandle;
}

MSH_CMD_EXPORT(rc_set, set rc);
ZINO_APP_EXPORT(rc_task_init);
